package com.cz.modules.cw.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.*;

import cn.hutool.core.date.DateUtil;
import com.cz.common.utils.ParkingUtil;
import com.cz.common.validator.ValidatorUtils;
import com.cz.modules.cw.entity.ParkingInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import com.cz.modules.cw.entity.CwParkingSpaceEntity;
import com.cz.modules.cw.service.CwParkingSpaceService;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 车位管理
 *
 * @author Cz
 * @email 
 * @date 2020-04-10 10:20:00
 */
@RestController
@RequestMapping("cw/cwparkingspace")
public class CwParkingSpaceController {
    @Autowired
    private CwParkingSpaceService cwParkingSpaceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("cw:cwparkingspace:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cwParkingSpaceService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 车位占用率统计 第三页面 上中
     */
    @RequestMapping("/cwStatistics")
    public R cwStatistics(@RequestParam Map<String, Object> params){
        Date date=new Date();
        String type= (String) params.get("type");
        String time= (String) params.get("time");
        String startTime="";
        String endTime="";
        if(!"".equals(time) && time !=null){//有自定义时间
            if("0".equals(type)){//该自定义时间 的当天的起止时间的数据
                startTime =time+" 00:00:00";
                endTime =time+" 23:59:59";
            }else if("1".equals(type)){//该自定义时间 的当月的起止时间的数据
                startTime =time+"-01 00:00:00";
                Date dd= DateUtil.parse(startTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(dd);
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 0);
                endTime = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
                endTime +=" 23:59:59";
            }
        }else {//无自定义时间
            if("0".equals(type)){//当天的数据
                endTime=DateUtil.format(date,"yyyy-MM-dd HH:mm:ss");
                startTime=DateUtil.format(date,"yyyy-MM-dd");
                startTime +=" 00:00:00";
            }else if("1".equals(type)){//当月的数据
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                // 获取前月的第一天
                calendar.add(Calendar.MONTH, 0);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                startTime= DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
                startTime +=" 00:00:00";
                endTime = DateUtil.format(date,"yyyy-MM-dd");
                endTime +=" 23:59:59";
            }
        }
        if("0".equals(type)){//小时
            return R.ok()
                    .put("southlist",cwParkingSpaceService.cwStatisticsHour("南区",startTime,endTime))
                    .put("northlist",cwParkingSpaceService.cwStatisticsHour("北区",startTime,endTime));
        }else if("1".equals(type)){//天
            return R.ok()
                    .put("southlist",cwParkingSpaceService.cwStatisticsDay("南区",startTime,endTime))
                    .put("northlist",cwParkingSpaceService.cwStatisticsDay("北区",startTime,endTime));
        }
        return R.ok();
    }/**
     * 车位占用率统计 第三页面 上左
     */
    @RequestMapping("/cwCount")
    public R cwCount(@RequestParam Map<String, Object> params){
        Date date=new Date();
        String type= (String) params.get("type");
        String time= (String) params.get("time");
        String startTime="";
        String endTime="";
        if(!"".equals(time) && time !=null){//有自定义时间
            if("0".equals(type)){//该自定义时间 的当天的起止时间的数据
                startTime =time+" 00:00:00";
                endTime =time+" 23:59:59";
            }else if("1".equals(type)){//该自定义时间 的当月的起止时间的数据
                startTime =time+"-01 00:00:00";
                Date dd= DateUtil.parse(startTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(dd);
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 0);
                endTime = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
                endTime +=" 23:59:59";
            }
        }else {//无自定义时间
            if("0".equals(type)){//当天的数据
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.HOUR_OF_DAY, 1);//1小时后
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                endTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd HH:mm:ss");
                startTime=DateUtil.format(date,"yyyy-MM-dd");
                startTime +=" 00:00:00";
            }else if("1".equals(type)){//当月的数据
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                // 获取前月的第一天
                calendar.add(Calendar.MONTH, 0);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                startTime= DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
                startTime +=" 00:00:00";
                // 获取当前月的最后一天
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(date);
                calendar1.add(Calendar.MONTH, 1);
                calendar1.set(Calendar.DAY_OF_MONTH, 0);
                endTime = DateUtil.format(calendar1.getTime(),"yyyy-MM-dd");
                endTime +=" 23:59:59";
            }
        }
        return R.ok().put("cwcountlist",cwParkingSpaceService.cwCountBytime(startTime,endTime));
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{pkid}")
    @RequiresPermissions("cw:cwparkingspace:info")
    public R info(@PathVariable("pkid") String pkid){
        CwParkingSpaceEntity cwParkingSpace = cwParkingSpaceService.getById(pkid);

        return R.ok().put("cwParkingSpace", cwParkingSpace);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("cw:cwparkingspace:save")
    public R save(@RequestBody CwParkingSpaceEntity cwParkingSpace){
        cwParkingSpaceService.save(cwParkingSpace);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("cw:cwparkingspace:update")
    public R update(@RequestBody CwParkingSpaceEntity cwParkingSpace){
        ValidatorUtils.validateEntity(cwParkingSpace);
        cwParkingSpaceService.updateById(cwParkingSpace);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("cw:cwparkingspace:delete")
    public R delete(@RequestBody String[] pkids){
        cwParkingSpaceService.removeByIds(Arrays.asList(pkids));

        return R.ok();
    }

    /**
     * 停車信息推送
     */
    @RequestMapping(value = "/parkingInfo", method = RequestMethod.POST)
    public void parkingInfo1( @RequestBody ParkingInfo info) throws IOException {
        System.err.println("车位信息接口测试-----------------"+info);
        if("A".equals(info.getServer_name())){//北区
            String key=null;
            if(info.getZone().startsWith("超长停车区")){
                key="超长停车区";
            }else if(info.getZone().startsWith("前广场大车区")){
                key="前广场大车区";
            }else if(info.getZone().startsWith("小车区")){
                key="小车区";
            }else if(info.getZone().startsWith("后广场货车区")){
                key="后广场货车区";
            }
            if(key!=null){
              if(ParkingUtil.northMap.containsKey(key)){
                  ParkingUtil.northMap.get(key).put(info.getZone(),Integer.parseInt(info.getVehicles()));
              }else {
                  Map<String,Integer> map=new HashMap<>();
                  ParkingUtil.northMap.put(key,map);
              }
            }
        }else if("B".equals(info.getServer_name())){
            String key=null;
            if(info.getZone().startsWith("超长停车区")){
                key="超长停车区";
            }else if(info.getZone().startsWith("前广场货车区")){
                key="前广场货车区";
            }else if(info.getZone().startsWith("前广场客车区")){
                key="前广场客车区";
            }else if(info.getZone().startsWith("小车区")){
                key="小车区";
            }else if(info.getZone().startsWith("后广场货车区")){
                key="后广场货车区";
            }
            if(key!=null){
                if(ParkingUtil.southMap.containsKey(key)){
                    ParkingUtil.southMap.get(key).put(info.getZone(),Integer.parseInt(info.getVehicles()));
                }else {
                    Map<String,Integer> map=new HashMap<>();
                    ParkingUtil.southMap.put(key,map);
                }
            }
        }
    }
    @RequestMapping("/spaceCount")
    public R spaceCount(){
        int p1=0,p2=0,p3=0,p4=0,p5=0;//每区剩余车位数
        int p1u = 0,p2u=0,p3u=0,p4u=0,p5u=0;//每区使用车位数
        Map<String,Integer> map1 = ParkingUtil.northMap.get("超长停车区");
        if(map1!=null){
            for(Map.Entry<String,Integer> en:map1.entrySet()){
                p1u += en.getValue();
            }
        }
        p1 = ParkingUtil.northCountMap.get("超长停车区")-p1u;
        Map<String,Integer> map2 = ParkingUtil.northMap.get("前广场货车区");
        if(map2!=null){
            for(Map.Entry<String,Integer> en:map2.entrySet()){
                p2u += en.getValue();
            }
        }
        p2 = ParkingUtil.northCountMap.get("前广场货车区")-p2u;

        Map<String,Integer> map3 = ParkingUtil.northMap.get("前广场客车区");
        if(map3!=null){
            for(Map.Entry<String,Integer> en:map3.entrySet()){
                p3u += en.getValue();
            }
        }
        p3 = ParkingUtil.northCountMap.get("前广场客车区")-p3u;

        Map<String,Integer> map4 = ParkingUtil.northMap.get("后广场货车区");
        if(map4!=null) {
            for (Map.Entry<String, Integer> en : map4.entrySet()) {
                p4u += en.getValue();
            }
        }
        p4 = ParkingUtil.northCountMap.get("后广场货车区")-p4u;
        Map<String,Integer> map5 = ParkingUtil.northMap.get("小车区");
        if(map5!=null) {
            for (Map.Entry<String, Integer> en : map5.entrySet()) {
                p5u += en.getValue();
            }
        }
        p5 = ParkingUtil.northCountMap.get("小车区")-p5u;

        int p6=0,p7=0,p8=0,p9=0,p10=0;//每区剩余车位数
        int p6u = 0,p7u=0,p8u=0,p9u=0,p10u=0;//每区使用车位数
        Map<String,Integer> map6 = ParkingUtil.southMap.get("超长停车区");
        if(map6!=null) {
            for (Map.Entry<String, Integer> en : map6.entrySet()) {
                p6u += en.getValue();
            }
        }
        p6 = ParkingUtil.southCountMap.get("超长停车区")-p6u;
       /* Map<String,Integer> map7 = ParkingUtil.southMap.get("前广场货车区");
        if(map7!=null) {
            for (Map.Entry<String, Integer> en : map7.entrySet()) {
                p7u += en.getValue();
            }
        }
        p7 = ParkingUtil.southCountMap.get("前广场大车区")-p7u;*/
        Map<String,Integer> map8 = ParkingUtil.southMap.get("前广场大车区");
        if(map8!=null) {
            for (Map.Entry<String, Integer> en : map8.entrySet()) {
                p8u += en.getValue();
            }
        }
        p8 = ParkingUtil.southCountMap.get("前广场大车区")-p8u;
        Map<String,Integer> map9 = ParkingUtil.southMap.get("后广场货车区");
        if(map9!=null) {
            for (Map.Entry<String, Integer> en : map9.entrySet()) {
                p9u += en.getValue();
            }
        }
        p9 = ParkingUtil.southCountMap.get("后广场货车区")-p9u;
        Map<String,Integer> map10 = ParkingUtil.southMap.get("小车区");
        if(map10!=null) {
            for (Map.Entry<String, Integer> en : map10.entrySet()) {
                p10u += en.getValue();
            }
        }
        p10 = ParkingUtil.southCountMap.get("小车区")-p10u;
        int pnk = p1 + p2 +p3 + p4 + p5;
        int psk = p6 + p7 + p8 + p9 + p10;
        int pn = ParkingUtil.northCountMap.get("超长停车区")+ParkingUtil.northCountMap.get("前广场货车区")+ParkingUtil.northCountMap.get("前广场客车区")+ParkingUtil.northCountMap.get("后广场货车区")+ParkingUtil.northCountMap.get("小车区");
        int ps = ParkingUtil.southCountMap.get("超长停车区")+ParkingUtil.southCountMap.get("前广场大车区")+ParkingUtil.southCountMap.get("后广场货车区")+ParkingUtil.southCountMap.get("小车区");
        int p = pn + ps ;
        int pk = pnk + psk;
        return R.ok().put("p",p).put("psk",psk).put("pnk",pnk).put("pk",pk);
    }
    @RequestMapping("/spaceShow")
    public R spaceShow(){
        int p1=0,p2=0,p3=0,p4=0,p5=0;//每区剩余车位数
        int p1u = 0,p2u=0,p3u=0,p4u=0,p5u=0;//每区使用车位数
        Map<String,Integer> map1 = ParkingUtil.northMap.get("超长停车区");
        if(map1!=null){
            for(Map.Entry<String,Integer> en:map1.entrySet()){
                p1u += en.getValue();
            }
        }
        p1 = ParkingUtil.northCountMap.get("超长停车区")-p1u;
        Map<String,Integer> map2 = ParkingUtil.northMap.get("前广场货车区");
        if(map2!=null){
            for(Map.Entry<String,Integer> en:map2.entrySet()){
                p2u += en.getValue();
            }
        }
        p2 = ParkingUtil.northCountMap.get("前广场货车区")-p2u;

        Map<String,Integer> map3 = ParkingUtil.northMap.get("前广场客车区");
        if(map3!=null){
            for(Map.Entry<String,Integer> en:map3.entrySet()){
                p3u += en.getValue();
            }
        }
        p3 = ParkingUtil.northCountMap.get("前广场客车区")-p3u;

        Map<String,Integer> map4 = ParkingUtil.northMap.get("后广场货车区");
        if(map4!=null) {
            for (Map.Entry<String, Integer> en : map4.entrySet()) {
                p4u += en.getValue();
            }
        }
        p4 = ParkingUtil.northCountMap.get("后广场货车区")-p4u;
        Map<String,Integer> map5 = ParkingUtil.northMap.get("小车区");
        if(map5!=null) {
            for (Map.Entry<String, Integer> en : map5.entrySet()) {
                p5u += en.getValue();
            }
        }
        p5 = ParkingUtil.northCountMap.get("小车区")-p5u;

        int p6=0,p7=0,p8=0,p9=0,p10=0;//每区剩余车位数
        int p6u = 0,p7u=0,p8u=0,p9u=0,p10u=0;//每区使用车位数
        Map<String,Integer> map6 = ParkingUtil.southMap.get("超长停车区");
        if(map6!=null) {
            for (Map.Entry<String, Integer> en : map6.entrySet()) {
                p6u += en.getValue();
            }
        }
        p6 = ParkingUtil.southCountMap.get("超长停车区")-p6u;
        Map<String,Integer> map7 = ParkingUtil.southMap.get("前广场货车区");
        if(map7!=null) {
            for (Map.Entry<String, Integer> en : map7.entrySet()) {
                p7u += en.getValue();
            }
        }
        p7 = ParkingUtil.southCountMap.get("前广场大车区")-p7u;
       /* Map<String,Integer> map8 = ParkingUtil.southMap.get("前广场客车区");
        if(map8!=null) {
            for (Map.Entry<String, Integer> en : map8.entrySet()) {
                p8u += en.getValue();
            }
        }
        p8 = ParkingUtil.southCountMap.get("前广场客车区")-p8u;*/
        Map<String,Integer> map9 = ParkingUtil.southMap.get("后广场货车区");
        if(map9!=null) {
            for (Map.Entry<String, Integer> en : map9.entrySet()) {
                p9u += en.getValue();
            }
        }
        p9 = ParkingUtil.southCountMap.get("后广场货车区")-p9u;
        Map<String,Integer> map10 = ParkingUtil.southMap.get("小车区");
        if(map10!=null) {
            for (Map.Entry<String, Integer> en : map10.entrySet()) {
                p10u += en.getValue();
            }
        }
        p10 = ParkingUtil.southCountMap.get("小车区")-p10u;

        return R.ok().put("p1",addZero(p1)).put("p2",addZero(p2)).put("p3",addZero(p3)).put("p4",addZero(p4)).put("p5",addZero(p5)).put("p6",addZero(p6)).put("p7",addZero(p7)).put("p8",addZero(p8)).put("p9",addZero(p9)).put("p10",addZero(p10));
    }
    public String addZero(int val){
        if(val<0){
            val = 0;
        }
        String valS = String.valueOf(val);
        if(valS.length()==1){
            valS = "00" + valS;
        }else if(valS.length()==2){
            valS = "0" + valS;
        }
        return valS;
    }
    //=========车位占用率
    @RequestMapping("/cwchart")
    public R cwchart(@RequestParam Map<String, Object> params){
        String type= (String) params.get("type");
        String startTime= (String) params.get("startTime");
        String endTime= (String) params.get("endTime");
        Calendar calendar = Calendar.getInstance();
        if("3".equals(type)){//查询页面上部的占用率
            DecimalFormat df = new DecimalFormat("0.00");
            //=====昨天=====
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            endTime = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
            endTime += " 23:59:59";
            startTime = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
            startTime += " 00:00:00";
            List listyess = cwParkingSpaceService.cwStatisticsDay("南区", startTime, endTime);
            List listyesn = cwParkingSpaceService.cwStatisticsDay("北区", startTime, endTime);
            List<String> yesslist = (List<String>)listyess.get(1);
            String yess = yesslist.get(0);
            List<String> yesnlist = (List<String>)listyesn.get(1);
            String yesn = yesnlist.get(0);
            String yes = df.format((Double.parseDouble(yess)+Double.parseDouble(yesn))/2);
            //======今天====
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            endTime = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
            endTime += " 23:59:59";
            startTime = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
            startTime += " 00:00:00";
            List listtodays = cwParkingSpaceService.cwStatisticsDay("南区", startTime, endTime);
            List listtodayn = cwParkingSpaceService.cwStatisticsDay("北区", startTime, endTime);
            List<String> todayslist = (List<String>)listtodays.get(1);
            String todays = todayslist.get(0);
            List<String> todaynlist = (List<String>)listtodayn.get(1);
            String todayn = todaynlist.get(0);
            String today = df.format((Double.parseDouble(todays)+Double.parseDouble(todayn))/2);
            //======本月====
            startTime = DateUtil.format(calendar.getTime(), "yyyy-MM");
            startTime += "-01 00:00:00";
            List listmonths = cwParkingSpaceService.cwStatisticsDay("南区", startTime, endTime);
            List listmonthn = cwParkingSpaceService.cwStatisticsDay("北区", startTime, endTime);
            List<String> sizelist = (List<String>)listmonths.get(0);
            Double zongs=0.00;
            Double zongn = 0.00;
            int size = sizelist.size();
            List<String> monthslist = (List<String>)listmonths.get(1);
            for(String s:monthslist){
                zongs += Double.parseDouble(s);
            }
            String months = df.format(zongs/size);
            List<String> monthnlist = (List<String>)listmonthn.get(1);;
            for(String s:monthnlist){
                zongn += Double.parseDouble(s);
            }
            String monthn = df.format(zongn/size);
            String month = df.format((zongn/size+zongs/size)/2);
            return R.ok().put("yesss",yes).put("yess",yess).put("yesn",yesn)
                    .put("today",today).put("todayn",todayn).put("todays",todays)
                    .put("monthss",month).put("monthsss",months).put("monthssn",monthn);
        }else {
            if ("0".equals(type)) { //近7天
                endTime = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
                endTime += " 23:59:59";
                calendar.add(Calendar.DAY_OF_MONTH, -6);
                startTime = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
                startTime += " 00:00:00";
            } else if ("1".equals(type)) {//近30天
                endTime = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
                endTime += " 23:59:59";
                calendar.add(Calendar.DAY_OF_MONTH, -29);
                startTime = DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
                startTime += " 00:00:00";
            }
            return R.ok()
                    .put("southlist", cwParkingSpaceService.cwStatisticsDay("南区", startTime, endTime))
                    .put("northlist", cwParkingSpaceService.cwStatisticsDay("北区", startTime, endTime));
        }
    }
}
